package com.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.reggie.Exception.CategoryException;
import com.reggie.dao.CategoryDao;
import com.reggie.dao.DishDao;
import com.reggie.pojo.Category;
import com.reggie.pojo.Dish;
import com.reggie.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    private CategoryDao categoryDao;

    @Autowired
    private DishDao dishDao;

    @Override
    public Boolean save(Category category) {
        return categoryDao.insert(category) > 0;
    }

    @Override
    public Page<Category> selectByPage(Integer page, Integer pageSize, Integer sort) {
        Page<Category> page_now = new Page<>(page, pageSize);
        LambdaQueryWrapper<Category> lqw = new LambdaQueryWrapper<>();
        lqw.orderByAsc(Category::getSort);
        categoryDao.selectPage(page_now, lqw);
        return page_now;
    }

    @Override
    public int DeleteById(Long ids) {

        if(dishDao.selectByCategoryId(ids).size() != 0){
            throw new CategoryException("这个类别中已经添加菜品了不能被删除");
        }
        System.out.println("没有菜品");
        return categoryDao.deleteById(ids);
    }

    @Override
    public int UpdateCategory(Category category) {
        LambdaQueryWrapper<Category> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Category::getId, category.getId());
        return categoryDao.update(category, lqw);
    }

    @Override
    public List<Category> SelectByType(Integer type) {
        return categoryDao.selectByType(type);
    }

    @Override
    public String selectCategoryNameById(Long id) {
        return categoryDao.selectCategoryNameById(id);
    }

    @Override
    public List<Category> selectAll() {
        LambdaQueryWrapper<Category> lqw = new LambdaQueryWrapper<>();
        lqw.orderByAsc(Category::getType);
        return categoryDao.selectList(lqw);
    }


}
